require File.dirname(__FILE__) + '/../../../lib/tasks/test_tasks'
require 'test/unit'
require 'mocha'
require File.dirname(__FILE__) + '/../../../lib/facades'

class WhenCallingMbUnitTask < Test::Unit::TestCase

  def setup
    @mbunit_dir = "./"
    @full_mbunit_dir = "#{File.expand_path(@mbunit_dir)}/"
    @test_files = "test.dll"
    @test_dir = "./test"
  end

  def test_should_fail_if_mbunit_dir_not_set
    begin
      Rubicant::Tasks::MbUnitTask.new('mb1')
      Rake.application['mb1'].invoke

    rescue RuntimeError => re
      assert_equal('mbunit_dir must be set', re.message)
    end
  end

  def test_should_fail_if_test_dir_not_set
    begin
      Rubicant::Tasks::MbUnitTask.new('mb2') do |mb|
        mb.mbunit_dir = @mbunit_dir
      end
      Rake.application['mb2'].invoke

    rescue RuntimeError => re
      assert_equal('test_dir must be set', re.message)
    end
  end

  def test_should_fail_if_test_files_not_set
    begin
      Rubicant::Tasks::MbUnitTask.new('mb3') do |mb|
        mb.mbunit_dir = @mbunit_dir
        mb.test_dir = @test_dir
      end
      Rake.application['mb3'].invoke

    rescue RuntimeError => re
      assert_equal('test_files must be set', re.message)
    end
  end

  def test_should_append_slash_if_mbunit_dir_doesnt_end_with_one
    Rubicant::Files.stubs(:copy)
    Rubicant::Files.stubs(:remove)
    Rubicant::Runner.expects(:run).with("MbUnit.Cons.exe #{@test_files}")

    Rubicant::Tasks::MbUnitTask.new('mb4') do |mb|
      mb.mbunit_dir = "."
      mb.test_dir = @test_dir
      mb.test_files = @test_files
    end

    Rake.application['mb4'].invoke
  end

  def test_should_set_mbunit_to_current_dir_if_mbunit_dir_empty
    Rubicant::Files.stubs(:copy)
    Rubicant::Files.stubs(:remove)
    Rubicant::Runner.expects(:run).with("MbUnit.Cons.exe #{@test_files}")

    Rubicant::Tasks::MbUnitTask.new('mb5') do |mb|
      mb.mbunit_dir = ""
      mb.test_dir = @test_dir
      mb.test_files = @test_files
    end

    Rake.application['mb5'].invoke
  end

  def test_should_set_a_single_assembly_path
    Rubicant::Files.stubs(:copy)
    Rubicant::Files.stubs(:remove)
    dll = "dll1.dll"
    Rubicant::Runner.expects(:run).with("MbUnit.Cons.exe /ap:#{dll} #{@test_files}")
    Rubicant::Tasks::MbUnitTask.new('mb6') do |mb|
      mb.mbunit_dir = @mbunit_dir
      mb.test_dir = @test_dir
      mb.test_files = @test_files
      mb.assembly_path = FileList[dll]
    end

    Rake.application['mb6'].invoke
  end

  def test_should_set_multiple_assembly_paths
    Rubicant::Files.stubs(:copy)
    Rubicant::Files.stubs(:remove)
    dll1 = "dll1.dll"
    dll2 = "dll2.dll"
    Rubicant::Runner.expects(:run).with("MbUnit.Cons.exe /ap:#{dll1} /ap:#{dll2} #{@test_files}")

    Rubicant::Tasks::MbUnitTask.new('mb7') do |mb|
      mb.mbunit_dir = @mbunit_dir
      mb.test_dir = @test_dir
      mb.test_files = @test_files
      mb.assembly_path = FileList[dll1, dll2]
    end

    Rake.application['mb7'].invoke
  end

  def test_should_set_report_folder
    Rubicant::Files.stubs(:copy)
    Rubicant::Files.stubs(:remove)
    report_folder = "./reports"
    Rubicant::Runner.expects(:run).with("MbUnit.Cons.exe /rf:#{report_folder} #{@test_files}")

    Rubicant::Tasks::MbUnitTask.new('mb21') do |mb|
      mb.mbunit_dir = @mbunit_dir
      mb.test_dir = @test_dir
      mb.test_files = @test_files
      mb.report_folder = report_folder
    end

    Rake.application['mb21'].invoke
  end

  def test_should_set_report_name_format
    Rubicant::Files.stubs(:copy)
    Rubicant::Files.stubs(:remove)
    report_format = "format{0}"
    Rubicant::Runner.expects(:run).with("MbUnit.Cons.exe /rnf:#{report_format} #{@test_files}")

    Rubicant::Tasks::MbUnitTask.new('mb8') do |mb|
      mb.mbunit_dir = @mbunit_dir
      mb.test_dir = @test_dir
      mb.test_files = @test_files
      mb.report_name_format = report_format
    end

    Rake.application['mb8'].invoke
  end

  def test_should_set_report_type
    Rubicant::Files.stubs(:copy)
    Rubicant::Files.stubs(:remove)
    report_type = "xml"
    Rubicant::Runner.expects(:run).with("MbUnit.Cons.exe /rt:#{report_type} #{@test_files}")
    Rubicant::Tasks::MbUnitTask.new('mb9') do |mb|
      mb.mbunit_dir = @mbunit_dir
      mb.test_dir = @test_dir
      mb.test_files = @test_files
      mb.report_type = report_type
    end

    Rake.application['mb9'].invoke
  end

  def test_should_set_show_reports
    Rubicant::Files.stubs(:copy)
    Rubicant::Files.stubs(:remove)
    show_reports = "+"
    Rubicant::Runner.expects(:run).with("MbUnit.Cons.exe /sr#{show_reports} #{@test_files}")
    Rubicant::Tasks::MbUnitTask.new('mb10') do |mb|
      mb.mbunit_dir = @mbunit_dir
      mb.test_dir = @test_dir
      mb.test_files = @test_files
      mb.show_reports = show_reports
    end

    Rake.application['mb10'].invoke
  end

  def test_should_set_transform
    Rubicant::Files.stubs(:copy)
    Rubicant::Files.stubs(:remove)
    transform = "transform"
    Rubicant::Runner.expects(:run).with("MbUnit.Cons.exe /tr:#{transform} #{@test_files}")
    Rubicant::Tasks::MbUnitTask.new('mb11') do |mb|
      mb.mbunit_dir = @mbunit_dir
      mb.test_dir = @test_dir
      mb.test_files = @test_files
      mb.transform = transform
    end

    Rake.application['mb11'].invoke
  end

  def test_should_set_filter_category
    Rubicant::Files.stubs(:copy)
    Rubicant::Files.stubs(:remove)
    filter_category = "filter_category"
    Rubicant::Runner.expects(:run).with("MbUnit.Cons.exe /fc:#{filter_category} #{@test_files}")
    Rubicant::Tasks::MbUnitTask.new('mb12') do |mb|
      mb.mbunit_dir = @mbunit_dir
      mb.test_dir = @test_dir
      mb.test_files = @test_files
      mb.filter_category = filter_category
    end

    Rake.application['mb12'].invoke
  end

  def test_should_set_exclude_category
    Rubicant::Files.stubs(:copy)
    Rubicant::Files.stubs(:remove)
    exclude_category = "exclude_category"
    Rubicant::Runner.expects(:run).with("MbUnit.Cons.exe /ec:#{exclude_category} #{@test_files}")
    Rubicant::Tasks::MbUnitTask.new('mb13') do |mb|
      mb.mbunit_dir = @mbunit_dir
      mb.test_dir = @test_dir
      mb.test_files = @test_files
      mb.exclude_category = exclude_category
    end

    Rake.application['mb13'].invoke
  end

  def test_should_set_filter_author
    Rubicant::Files.stubs(:copy)
    Rubicant::Files.stubs(:remove)
    filter_author = "filter_author"
    Rubicant::Runner.expects(:run).with("MbUnit.Cons.exe /fa:#{filter_author} #{@test_files}")
    Rubicant::Tasks::MbUnitTask.new('mb14') do |mb|
      mb.mbunit_dir = @mbunit_dir
      mb.test_dir = @test_dir
      mb.test_files = @test_files
      mb.filter_author = filter_author
    end

    Rake.application['mb14'].invoke
  end

  def test_should_set_filter_namespace
    Rubicant::Files.stubs(:copy)
    Rubicant::Files.stubs(:remove)
    filter_namespace = "filter_namespace"
    Rubicant::Runner.expects(:run).with("MbUnit.Cons.exe /fn:#{filter_namespace} #{@test_files}")
    Rubicant::Tasks::MbUnitTask.new('mb15') do |mb|
      mb.mbunit_dir = @mbunit_dir
      mb.test_dir = @test_dir
      mb.test_files = @test_files
      mb.filter_namespace = filter_namespace
    end

    Rake.application['mb15'].invoke
  end

  def test_should_set_verbose
    Rubicant::Files.stubs(:copy)
    Rubicant::Files.stubs(:remove)
    verbose ="verbose"
    Rubicant::Runner.expects(:run).with("MbUnit.Cons.exe /v#{verbose} #{@test_files}")
    Rubicant::Tasks::MbUnitTask.new('mb16') do |mb|
      mb.mbunit_dir = @mbunit_dir
      mb.test_dir = @test_dir
      mb.test_files = @test_files
      mb.verbose = verbose
    end

    Rake.application['mb16'].invoke
  end

  def test_should_set_shadow_copy
    Rubicant::Files.stubs(:copy)
    Rubicant::Files.stubs(:remove)
    shadow_copy = "shadow_copy"
    Rubicant::Runner.expects(:run).with("MbUnit.Cons.exe /sc#{shadow_copy} #{@test_files}")
    Rubicant::Tasks::MbUnitTask.new('mb17') do |mb|
      mb.mbunit_dir = @mbunit_dir
      mb.test_dir = @test_dir
      mb.test_files = @test_files
      mb.shadow_copy = shadow_copy
    end

    Rake.application['mb17'].invoke
  end

  def test_should_copy_mbunit_files_when_run
    Rubicant::Files.expects(:copy).with("#{@full_mbunit_dir}QuickGraph.dll", @test_dir)
    Rubicant::Files.expects(:copy).with("#{@full_mbunit_dir}QuickGraph.Algorithms.dll", @test_dir)
    Rubicant::Files.expects(:copy).with("#{@full_mbunit_dir}MbUnit.Framework.dll", @test_dir)
    Rubicant::Files.expects(:copy).with("#{@full_mbunit_dir}MbUnit.Cons.exe", @test_dir)
    Rubicant::Files.expects(:copy).with("#{@full_mbunit_dir}MbUnit.Cons.exe.config", @test_dir)
    Rubicant::Files.stubs(:remove)
    Rubicant::Runner.stubs(:run)
    Rubicant::Tasks::MbUnitTask.new('mb18') do |mb|
      mb.mbunit_dir = @mbunit_dir
      mb.test_dir = @test_dir
      mb.test_files = @test_files
    end

    Rake.application['mb18'].invoke
  end

  def test_should_remove_mbunit_files_when_done
    Rubicant::Files.expects(:remove).with("#{@test_dir}/QuickGraph.dll")
    Rubicant::Files.expects(:remove).with("#{@test_dir}/QuickGraph.Algorithms.dll")
    Rubicant::Files.expects(:remove).with("#{@test_dir}/MbUnit.Framework.dll")
    Rubicant::Files.expects(:remove).with("#{@test_dir}/MbUnit.Cons.exe")
    Rubicant::Files.expects(:remove).with("#{@test_dir}/MbUnit.Cons.exe.config")
    Rubicant::Files.stubs(:copy)
    Rubicant::Runner.stubs(:run)
    Rubicant::Tasks::MbUnitTask.new('mb19') do |mb|
      mb.mbunit_dir = @mbunit_dir
      mb.test_dir = @test_dir
      mb.test_files = @test_files
    end

    Rake.application['mb19'].invoke
  end

  def test_should_change_working_directory_to_test_directory
    Rubicant::Files.stubs(:copy)
    Rubicant::Files.stubs(:remove)
    Rubicant::Runner.stubs(:run)
    Dir.expects(:chdir).with(@test_dir)
    Rubicant::Tasks::MbUnitTask.new('mb20') do |mb|
      mb.mbunit_dir = @mbunit_dir
      mb.test_dir = @test_dir
      mb.test_files = @test_files
    end

    Rake.application['mb20'].invoke
  end


end
